﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LocumUpdate
{
    public class HttpWebDownload : Baglanti
    {
        HttpWebRequest reqHttp;

        public HttpWebDownload(string userName, string userPassword, string siteAdresi)
        {
            this.UserName = userName;
            this.UserPassword = userPassword;
            this.SiteAdresi = siteAdresi;
        }

        public Stream getStreamDownload(string filePath, string fileName)
        {
            FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

            reqHttp = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://" + this.SiteAdresi + "/LocumExe/" + fileName));
            reqHttp.Method = WebRequestMethods.Http.Get;
            // reqFTP.UseBinary = true;
            reqHttp.Credentials = new NetworkCredential(this.UserName, this.UserPassword);
            HttpWebResponse response = (HttpWebResponse)reqHttp.GetResponse();
            Stream ftpStream = response.GetResponseStream();

            Download(filePath, fileName, ftpStream, outputStream);

            response.Close();

            return ftpStream;
        }

        public string[] GetFileList(string versionConfig, StreamReader reader)
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            try
            {
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }

                result.Remove(result.ToString().LastIndexOf('\n'), 1);
                reader.Close();

                downloadFiles = result.ToString().Split('\n');
            }
            catch (Exception)
            {
             
                downloadFiles = null;
            }
            return downloadFiles;
        }

        public string[] getStreamGetList(string versionConfig)
        { 
               
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
          
            try
            {//Locumexe/
                reqHttp = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://" + this.SiteAdresi + "/Locumexe/" + versionConfig));
               //  reqHttp.UseBinary = true;
                reqHttp.Credentials = new NetworkCredential(this.UserName, this.UserPassword);
          
                reqHttp.Method = WebRequestMethods.Http.Get;

                WebResponse response = reqHttp.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);

                downloadFiles= GetFileList(versionConfig, reader);
                response.Close();
            }     
            catch (Exception)
            {              
                downloadFiles = null;                
            }
            return downloadFiles;
        }

        public HttpWebDownload()
        {

        }

    }
}
